package miage.m1.gui;

import miage.m1.launch.Config;
import miage.m1.plugins.PluginLoader;
import miage.m1.plugins.PluginMiniExcel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Iterator;


/**
 * Created with IntelliJ IDEA.
 * User: tm005197
 * Date: 12/27/12
 * Time: 2:11 PM
 * To change this template use File | Settings | File Templates.
 */
public class mainwindow extends JFrame implements ActionListener {
    private JPanel topPanel;
    private JPanel mainPanel;
    private JPanel bottomPanel;
    private JMenuBar menuBar;


    public mainwindow() {
        super((String) Config.getParameterByName("applicationname"));
        this.menuBar = new JMenuBar();
        JMenu menu = new JMenu("Fichier");
        JMenuItem item = new JMenuItem("Quitter");
        item.addActionListener(this);
        menuBar.add(menu).add(item);
        initComponents();
        JMenu menu2 = new JMenu("?");
        JMenuItem about = new JMenuItem("A propos");
        about.addActionListener(this);
        menuBar.add(menu2).add(about);
        this.setJMenuBar(this.menuBar);

        WindowListener exitListener = new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {

                closeApplication();
            }
        };
        this.addWindowListener(exitListener);


        pack();
        setVisible(true);
    }

    /**
     * on initialise les plugins
     */
    private void initPlugins() {
        ArrayList<PluginMiniExcel> plugins = PluginLoader.getInstance().getAllPlugins();
        System.out.println(plugins.size());
        if (PluginLoader.getInstance() == null)
            System.err.println("NULL----------------");
        // on prend chaque plugin ,un par un
        for (Iterator<PluginMiniExcel> pl = PluginLoader.getInstance().getAllPlugins().iterator(); pl.hasNext(); ) {
            PluginMiniExcel plugin = pl.next();
            plugin.initialise();                      // on les initialise
            // et on ajoute leur affichage s'il en ont
            if (plugin.getTopDisplay() != null) {
                topPanel.add(plugin.getTopDisplay());
            }
            if (plugin.getMainDisplay() != null) {
                mainPanel.add(plugin.getMainDisplay(),this.cons);
            }
            if (plugin.getBottomDisplay() != null) {
                mainPanel.add(plugin.getBottomDisplay());
            }
            if (plugin.getMenu() != null) {
                this.menuBar.add(plugin.getMenu());
            }
        }
    }

    private  GridBagConstraints cons;
    private void initComponents() {
        // on initialise l'affichage
        topPanel = new JPanel();
        mainPanel = new JPanel();
        bottomPanel = new JPanel();
        mainPanel.setLayout(new GridBagLayout());
        cons = new GridBagConstraints();
        cons.fill = GridBagConstraints.HORIZONTAL;
        cons.weightx = 1;
        cons.gridx = 0;



        topPanel.setLayout(new BoxLayout(topPanel, 0));
        bottomPanel.setLayout(new BoxLayout(bottomPanel, 0));
        // on initialise les plugin et ajoute leur affichage correspondants
        initPlugins();

        setLayout(new BorderLayout());
        add(topPanel, BorderLayout.NORTH);
        add(mainPanel, BorderLayout.CENTER);
        add(bottomPanel, BorderLayout.SOUTH);
    }


    public static void main(String[] args) {
        new mainwindow();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // if menu click
        if (e.getSource() instanceof JMenuItem) {
            if (((JMenuItem) e.getSource()).getText().equals("Quitter")) {
                closeApplication();
            } else if (((JMenuItem) e.getSource()).getText().equals("A propos")) {

                JOptionPane.showMessageDialog(null, "<html><body width=\"500px\"><h1>" + Config.getParameterByName("applicationname") + "</h1><br/>" + ((String) Config.getParameterByName("aboutapplication")).replace("\\n", "<br/>") + "</body></html>", "A propos", JOptionPane.PLAIN_MESSAGE);
            }
        }
    }

    public void closeApplication() {

        if (JOptionPane.showConfirmDialog(null, "Etes vous sure ?", "Confirmation de Fermeture", JOptionPane.YES_NO_OPTION) == 0) {
            System.exit(0);
        }
    }
}
